package org.cycuchy.hp.data {
	
	import flash.utils.getTimer;
	
	import mx.collections.ArrayCollection;
	
	public class FotosData implements IDatas {
		
		public var rows:uint;
		public var cols:uint;
		
		public var verticalGap:uint;
		public var horizontalGap:uint;
		
		public var scaleY:Number;
		public var scaleX:Number;
		public var polaroidWidth:uint;
		public var polaroidHeight:uint;
		
		public var fotos:ArrayCollection;
		
		public function get dataLength():int {
			return fotos.length;
		}
		
		public function get elementsOnPage():int {
			return rows * cols;
		}
		
		
		public static function xml2KapstelkiData(xml:XML):FotosData {
			var data:FotosData = new FotosData();	
			data.rows = xml.@rows;
			data.cols = xml.@cols;
			data.verticalGap = xml.@verticalGap;
			data.horizontalGap = xml.@horizontalGap;
			data.scaleX = xml.@scaleX;
			data.scaleY = xml.@scaleY;
			data.polaroidWidth = xml.@polaroidWidth;
			data.polaroidHeight = xml.@polaroidHeight;
			var fotos:ArrayCollection = new ArrayCollection();

			var foto:FotoData;
			var timer:int = getTimer();
			var fotosPerPage:uint = data.rows * data.cols;
			var empty:uint;
			var mod:uint;
			var galleryName:String;
			for each(var k:XML in xml.galleries.gallery) {
				galleryName = k.@name;
				for each(var f:XML in k.foto) {
					foto = new FotoData();
					foto.galleryName = galleryName;
					foto.id = f.@id;
					foto.imgSrc = f.@imgSrc + "?x=" + timer;
					fotos.addItem(foto);				
				}
				mod = fotos.length%fotosPerPage;
				if (mod != 0) {
					empty = fotosPerPage - mod;
					for (var i:uint=0; i<empty; i++) {
						fotos.addItem(null);
					}					
				}
			}
			data.fotos = fotos;
			return data;
		}	
			
	}
	
}